package models

import (
	"fmt"
	"log"
	"rongyy/src/app"
)

type CaseModel struct {
	BaseModel           `xorm:"extends"`
	ApplySn             string        `json:"apply_sn"`   // 状态 0启用 1停用
	OrderSn             string        `json:"order_sn"`   // 状态 0启用 1停用
	Fee                 string        `json:"fee"`        // 缴费费用
	PatientId           string        `json:"patient_id"` // 0 默认值 1删除
	RefundSn            string        `json:"refund_sn"`  // 创建人
	Status              string        `json:"status"`     // 0 待支付，1挂号成功，-1退号，-2退号退费
	PayStatus           string        `json:"pay_status"` // 0 创建 -1支付失败 1支付中 2支付成功
	UpdatedBy           string        `json:"updated_by"` //callback updated by
	Remark              string        `json:"remark"`
	AdmNo               string        `json:"adm_no"`
	Address             string        `json:"address"`
	Openid              string        `json:"openid" xorm:"openid"`
	DoctorName          string        `json:"doctor_name"`
	PatientName         string        `json:"patient_name"`
	CopyAt              string        `xorm:"copy_at" json:"copy_at"`
	Mobile              string        `json:"mobile"`
	ContactName         string        `json:"contact_name"`
	IdImgs              []string      `json:"id_imgs"`
	BirthImgs           []string      `json:"birth_imgs"`
	ProxyIdImgs         []string      `json:"proxy_id_imgs"`
	ProxyImgs           []string      `json:"proxy_imgs"`
	AuthImgs            []string      `json:"auth_imgs"`
	OperatorCode        string        `json:"operator_code"`
	ApplyType           string        `json:"apply_type"`
	Project             []CaseProject `json:"project"`
	ProjectExtra        []CaseProject `json:"project_extra"`
	DeliverySn          string        `json:"delivery_sn"`
	DeliveryCompanyName string        `json:"delivery_company_name"`
	DisDateNurse        string        `json:"dis_date_nurse"`
	DisTimeNurse        string        `json:"dis_time_nurse"`
	Other               string        `json:"other"`
}

type CaseProject struct {
	Title string `json:"title"`
	Count int64  `json:"count"`
}

type ReqCase struct {
	Limit     int    `json:"limit"`
	Offset    int    `json:"offset"`
	PatientId string `json:"patient_id"`
	OrderSn   string `json:"order_sn"`
	Status    string `json:"status"`
}

func (rm *CaseModel) TableName() string {
	return "case_delivery"
}

func (rm *CaseModel) Create(req *CaseModel) error {
	_, err := app.DB().Insert(req)
	if err != nil {
		log.Println(err)
		return err
	}
	return nil
}

func (rm *CaseModel) GetById(id int64) (*CaseModel, error) {
	ncm := &CaseModel{}
	has, err := app.DB().ID(id).Get(ncm)
	if !has {
		return nil, fmt.Errorf("不存在")
	}
	if err != nil {
		return nil, err
	}
	return ncm, nil
}

func (rm *CaseModel) FindByOrderSn(req CaseModel) (*CaseModel, error) {
	ncm := &CaseModel{}
	has, err := app.DB().Where("order_sn=?", req.OrderSn).Get(ncm)
	if !has {
		return nil, fmt.Errorf("不存在")
	}
	if err != nil {
		return nil, err
	}
	return ncm, nil
}

func (rm *CaseModel) FindByAdmNo(req *CaseModel) (*CaseModel, bool, error) {
	ncm := &CaseModel{}
	has, err := app.DB().Where("adm_no=? and patient_id=?", req.AdmNo, req.PatientId).Get(ncm)

	if err != nil {
		return nil, false, err
	}
	return ncm, has, nil
}

func (rm *CaseModel) FindBySn(req *CaseModel) (*CaseModel, bool, error) {
	ncm := &CaseModel{}
	has, err := app.DB().Where("apply_sn=?", req.ApplySn).Get(ncm)

	if err != nil {
		return nil, false, err
	}
	return ncm, has, nil
}

func (rm *CaseModel) Query(req *ReqCase) ([]CaseModel, int64, error) {
	cms := make([]CaseModel, 0)
	//local, _ := time.LoadLocation("Asia/Shanghai")
	//t := time.Now().In(local).Format("2006-01-02 15:04:05")
	fmt.Println(req.Status != "")
	s := app.DB().Table("case_delivery")
	if req.PatientId != "" {
		pid := fmt.Sprintf("%%%s%%", req.PatientId)
		s.Where("patient_id like ?", pid)
	}

	if req.OrderSn != "" {
		orderSn := fmt.Sprintf("%%%s%%", req.OrderSn)
		s.Where("order_sn like ?", orderSn)
	}

	if req.Status != "" {
		s.Where("status = ?", req.Status)
	}

	total, err := s.Limit(req.Limit, req.Offset).Desc("created_at").FindAndCount(&cms)
	if err != nil {
		return nil, 0, err
	}
	return cms, total, err
}

func (rm *CaseModel) UpdateId() error {
	s := app.DB()
	_, err := s.ID(rm.Id).Update(rm)
	if err != nil {
		return err
	}
	return nil
}

func (rm *CaseModel) UpdateAdmNo() error {
	s := app.DB()
	_, err := s.Where("adm_no=?", rm.AdmNo).Update(rm)
	if err != nil {
		return err
	}
	return nil
}

func (rm *CaseModel) UpdateOrderSn() error {
	s := app.DB()
	_, err := s.Where("order_sn=?", rm.OrderSn).Update(rm)
	if err != nil {
		return err
	}
	return nil
}

func (rm *CaseModel) UpdateApplySn() error {
	s := app.DB()
	_, err := s.Where("apply_sn=?", rm.ApplySn).Update(rm)
	if err != nil {
		return err
	}
	return nil
}

func (rm *CaseModel) ConfirmPay() error {
	s := app.DB()
	_, err := s.Where("order_sn=? and apply_sn=?", rm.OrderSn, rm.ApplySn).Update(rm)
	if err != nil {
		return err
	}
	return nil
}
